from typing import List
from .model import UsersAuth
from ninja import Field, ModelSchema, Query, Router
from ninja.pagination import paginate
from utils.fu_crud import (
    ImportSchema,
    create,
    delete,
    export_data,
    import_data,
    retrieve,
    update,
)
from utils.fu_ninja import FuFilters, MyPagination

router = Router()


# 设置过滤字段
class Filters(FuFilters):
        
    diff_approval: bool = Field(None, alias='diff_approval')    
    product_group: str = Field(None, alias='product_group')    
    manual_delivery: bool = Field(None, alias='manual_delivery')    
    order_types: str = Field(None, alias='order_types')    
    display_price: bool = Field(None, alias='display_price')    
    product_types: str = Field(None, alias='product_types')    
    user_id: str = Field(None, alias='user_id')


# 设置请求接收字段
class UsersAuthSchemaIn(ModelSchema):

    class Config:
        model = UsersAuth
        model_exclude = ['create_datetime', 'update_datetime', 'belong_dept', 'modifier', 'creator', 'id']


# 设置响应字段
class UsersAuthSchemaOut(ModelSchema):

    class Config:
        model = UsersAuth
        model_fields = '__all__'


# 创建UsersAuth
@router.post('/users_auth', response=UsersAuthSchemaOut)
def create_users_auth(request, data: UsersAuthSchemaIn):
    users_auth = create(request, data, UsersAuth)
    return users_auth


# 删除UsersAuth
@router.delete('/users_auth/{id}')
def delete_users_auth(request, id: int):
    delete(id, UsersAuth)
    return {'success': True}


# 更新UsersAuth
@router.put('/users_auth/{id}', response=UsersAuthSchemaOut)
def update_users_auth(request, id: int, data: UsersAuthSchemaIn):
    users_auth = update(request, id, data, UsersAuth)
    return users_auth


# 获取UsersAuth
@router.get('/users_auth', response=List[UsersAuthSchemaOut])
@paginate(MyPagination)
def list_users_auth(request, filters: Filters = Query(...)):
    qs = retrieve(request, UsersAuth, filters)
    return qs


# 导入
@router.get('/users_auth/all/export')
def export_users_auth(request):
    export_fields = ('diff_approval', 'product_group', 'manual_delivery', 'order_types', 'display_price', 'product_types', 'user_id', )
    return export_data(request, UsersAuth, UsersAuthSchemaOut, export_fields)


# 导出
@router.post('/users_auth/all/import')
def import_users_auth(request, data: ImportSchema):
    import_fields = ('diff_approval', 'product_group', 'manual_delivery', 'order_types', 'display_price', 'product_types', 'user_id', )
    return import_data(request, UsersAuth, UsersAuthSchemaIn, data, import_fields)
    